using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.Xml;
using System.Globalization;

namespace MasomRecieptBookApplication
{
    public partial class Form1 : Form
    {
        string path = "MasomRecords.xml";
        public DataGridView TheDataGridView = new DataGridView();
        public static DataSet MasomData = new DataSet();
        public static DataTable MasomTable = new DataTable("MasomMembers");
        public static DataView MasomView = new DataView();
        bool edit = false;
        static int noRows = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            InitializeTable();
        }

        private void Form1_Deactivate(object sender, EventArgs e)
        {
            MasomData.WriteXml(path, XmlWriteMode.WriteSchema);
        }

        private void BlankOut()
        {
            txtboxName.Text = "";
            dateTimePicker1.Text = "";
            txtboxAmount.Text = "";
            checkCash.Checked = false;
            txtboxCheck.Text = "";
            txtboxReciept.Text = "";
            cbItems.Text = "";
        }

        private void InitializeTable()
        {
            MasomData.Tables.Add(MasomTable);
            MasomTable.Columns.Add("Name", typeof(string));
            MasomTable.Columns.Add("Date", typeof(DateTime));
            MasomTable.Columns.Add("Amount", typeof(double));
            MasomTable.Columns.Add("Cash", typeof(bool));
            MasomTable.Columns.Add("Check", typeof(string));
            MasomTable.Columns.Add("Reciept", typeof(string));
            MasomTable.Columns.Add("Types", typeof(string));
            MasomView = MasomData.Tables["MasomMembers"].DefaultView;
            gridInfo.DataSource = MasomData.Tables["MasomMembers"];
            TotalsOnComboBoxWork();
            if (File.Exists(path))
            {
                MasomData.ReadXml(path, XmlReadMode.ReadSchema);
                gridInfo.DataSource = MasomData.Tables["MasomMembers"];
                RefreshTotals();
            }
        }

        private void TotalsOnComboBoxWork()
        {
            object[] temp = new object[cbItems.Items.Count];
            cbItems.Items.CopyTo(temp, 0);
            cbTotalToType.Items.AddRange(temp);
        }

        private void RefreshTotals()
        {
            lblAmount.Text = "The Amount of Rows: " + MasomView.Count;
            lblTotal.Text = "$" + MasomTable.Compute("Sum(Amount)", "");
            string check = "Types = '" + cbTotalToType.Text + "'";
            lblTotalToType.Text = "$" + MasomTable.Compute("Sum(Amount)", check);
            txtboxName.Focus();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            gridInfo.ReadOnly = true;
            if (txtboxName.Text == "" || txtboxReciept.Text == "")
            {
                MessageBox.Show("You Must Have A Name AND Reciept, Please Redo", "Name Reciept Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                BlankOut();
            }
            else
            {
                if (checkCash.Checked == true && txtboxCheck.Text != "")
                {
                    MessageBox.Show("If You Have Cash You Cannot Have A Check Number, Please Redo", "Cash Check Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    BlankOut();
                }
                else
                {
                    DataRow MasomRow = MasomTable.NewRow();
                    MasomRow["Name"] = txtboxName.Text;
                    DateTime dt = dateTimePicker1.Value;
                    MasomRow["Date"] = dt.ToLongDateString();
                    MasomRow["Cash"] = checkCash.CheckState;
                    MasomRow["Check"] = txtboxCheck.Text;
                    MasomRow["Reciept"] = txtboxReciept.Text;
                    string selectedType = (string)cbItems.SelectedItem;
                    if (cbItems.FindStringExact(selectedType) == -1)
                    {
                         cbItems.Items.Add(cbItems.Text);
                         cbTotalToType.Items.Add(cbItems.Text);
                    }
                    MasomRow["Types"] = cbItems.Text;
                    try
                    {
                        MasomRow["Amount"] = Convert.ToDouble(txtboxAmount.Text);
                        MasomTable.Rows.Add(MasomRow);
                        gridInfo.DataSource = MasomData.Tables["MasomMembers"];
                        BlankOut();
                        RefreshTotals();
                    }
                    catch (Exception err)
                    {
                        MessageBox.Show("Amount Has Not Been Input Properly, Please Redo", "Amount Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                        BlankOut();
                    }
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (edit == false)
            {
                edit = true;
                gridInfo.ReadOnly = false;
                btnEdit.Text = "End Edit";
            }
            else
            {
                edit = false;
                gridInfo.ReadOnly = true;
                btnEdit.Text = "To Edit";
                RefreshTotals();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MasomView.Count > noRows)
            {
                DialogResult result = MessageBox.Show("Are You Sure ?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                if (result == DialogResult.Yes)
                {
                    MasomView.Delete(gridInfo.CurrentCell.RowIndex);
                    RefreshTotals();
                }
            }
            else
            {
                MessageBox.Show("No Rows To Delete, Please Add Rows or Select A Row", "Row Delete Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        private void btnASC_Click(object sender, EventArgs e)
        {
            try
            {
                string sortBy = cbSortItems.Text + " ASC";
                MasomView.Sort = sortBy;
            }
            catch (Exception err)
            {
            }
        }

        private void btnDSC_Click(object sender, EventArgs e)
        {
            try
            {
                string sortBy = cbSortItems.Text + " DESC";
                MasomView.Sort = sortBy;
            }
            catch (Exception err)
            {
            }
        }

        private void cbTotalToType_SelectedIndexChanged(object sender, EventArgs e)
        {
            string check = "Types = '" + cbTotalToType.Text + "'";
            lblTotalToType.Text = "$" + MasomTable.Compute("Sum(Amount)", check);
        }

        private void btnPrintGrid_Click(object sender, EventArgs e)
        {
            //Form2 frm = new Form2(MasomData);
            //Form2 frm = new Form();
            //frm.ShowDialog();
            //Application.Run(new Form2());
            MainForm from = new MainForm(ref MasomData);
            //Form2 from = new Form2(ref MasomData);
            from.ShowDialog();
        }

        private void gridInfo_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}